package com.qfedu.service.impl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.RouteEntity;
import com.qfedu.entity.RouteGroupEntity;
import com.qfedu.mapper.RouteMapper;
import com.qfedu.service.BoatService;
import com.qfedu.service.RouteService;
import com.qfedu.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class RouteServiceImpl implements RouteService {

    @Autowired(required = false)
    RouteMapper routeMapper;

    @Reference(check = false)
    BoatService boatService;


    public PageInfo<RouteEntity> queryList(RouteEntity re){


        PageHelper.startPage(re.getPageNum(), re.getPageSize());

        List<RouteEntity> list = routeMapper.queryList(re);


        for(RouteEntity route : list) {
            Date time = route.getTime();
            if (time != null){

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sdf.format(time);
                route.setTimes(format);
            }

        }

        PageInfo<RouteEntity> pageInfo = new PageInfo<RouteEntity>(list);

        return pageInfo;
    }

    public List<RouteGroupEntity> queryGroupList(){
        List<RouteGroupEntity> list = routeMapper.queryGroupList();
        return list;
    }

    public List<RouteEntity> queryByAddress(RouteEntity re){
        List<RouteEntity> list = routeMapper.queryList(re);

        return  list;

    }

    public List<RouteGroupEntity> queryGrList(RouteGroupEntity rgp){
        return routeMapper.queryGrList(rgp);
    }

    public RouteGroupEntity queryGroupById(int rgid){
        return routeMapper.queryGroupById(rgid);
    }

    public RouteEntity queryRouteById(int rid){
        RouteEntity routeEntity = routeMapper.queryRouteById(rid);

        return routeEntity;
    }

    public List<RouteEntity> queryRouteList(RouteEntity re){
        return routeMapper.queryList(re);
    }

    public boolean del(int rid){

       String str = "("+rid+")";

        return routeMapper.del(str);
    }


    public boolean deAll(String ids){

        String str = "("+ids.substring(0,ids.length()-1)+")";

        return routeMapper.del(str);
    }


    public boolean update(RouteEntity re){

        if (re.getTime() == null) {
            try {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Date time = sdf.parse(re.getTimes());

                re.setTime(time);

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        boolean m = boatService.insert(re.getRid(), re.getSid());

        return routeMapper.update(re);
    }


    public boolean insert(RouteEntity re){

        re.setRstate(2);


        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date time = sdf.parse(re.getTimes());

            re.setTime(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        boolean insert = routeMapper.insert(re);

        boolean m = boatService.insert(re.getRid(), re.getSid());

        if (insert && m){
            return true;
        }else {
            return false;
        }

    }


    public boolean insertBoatRoute(int rid,int sid){
        if (rid != 0 || sid != 0){
            return routeMapper.insertBoatRoute(rid,sid);
        }
        return false;
    }

    public List<RouteEntity> queryRouteList(){
        return routeMapper.queryRouteList();
    }

    public boolean delgp(int rgid){

        String str = "("+rgid+")";

        return routeMapper.delGroup(str);
    }


    public boolean delAllboatgroup(String ids){

        String str = "("+ids.substring(0,ids.length()-1)+")";

        return routeMapper.delGroup(str);
    }

    public boolean insertGroup(RouteGroupEntity rgp){
        return routeMapper.insertGroup(rgp);
    }

    public boolean updateGroup(RouteGroupEntity rgp){
        return routeMapper.updateGroup(rgp);
    }
}
